package com.ibeeking.found.upms.g.rest.controller.v1;

import com.ibeeking.found.common.annotation.LoginUser;
import com.ibeeking.found.common.entity.CurrentUser;
import com.ibeeking.found.upms.g.rest.service.IGmcPermissionResourceService;
import com.ibeeking.found.upms.g.rest.service.IGmcResourceService;
import com.ibeeking.found.upms.service.common.bo.PermissionResourceBO;
import com.ibeeking.found.upms.service.common.param.ResourceParam;
import com.ibeeking.found.upms.service.common.query.ResourceQuery;
import com.ibeeking.found.upms.service.common.vo.ResourceEffectiveVO;
import com.ibeeking.found.upms.service.common.vo.ResourceTreeVO;
import com.ibeeking.found.upms.service.common.vo.ResourceVO;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.utils.base.BaseValidationGroup;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName ResourceController控制器
 * @Description
 * @Author ibeeking
 * @Date 2020-12-07 09:09
 **/
@RestController
@RequestMapping("/g/resource/v1")
public class GmcResourceController {

    @Resource
    private IGmcResourceService resourceServiceImpl;
    @Resource
    private IGmcPermissionResourceService permissionResourceServiceImpl;

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询资源树列表")
    @PostMapping(value = "/queryList")
    public List<ResourceTreeVO> queryList(@RequestBody @Validated ResourceQuery resourceQuery) {
        return resourceServiceImpl.queryList(resourceQuery);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询有效资源列表树")
    @GetMapping(value = "/queryEffectiveList")
    public List<ResourceEffectiveVO> queryEffectiveList() {
        return resourceServiceImpl.queryEffectiveList();
    }

    @Log(logType = OperateLogTypeEnum.SAVE, describe = "资源新增")
    @PostMapping(value = "/add")
    public Boolean add(@LoginUser CurrentUser currentUser, @RequestBody @Validated(BaseValidationGroup.Insert.class) ResourceParam resourceParam) {
        resourceParam.setCreateBy(currentUser.getId());
        return resourceServiceImpl.add(resourceParam);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "资源修改")
    @PostMapping(value = "/modify")
    public Boolean modify(@LoginUser CurrentUser currentUser, @RequestBody @Validated(BaseValidationGroup.Update.class) ResourceParam resourceParam) {
        resourceParam.setModifyBy(currentUser.getId());
        return resourceServiceImpl.modify(resourceParam);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "资源查看")
    @GetMapping(value = "/view/{id}")
    public ResourceVO view(@PathVariable("id") Long id) {
        return resourceServiceImpl.view(id);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "资源启用/禁用")
    @GetMapping(value = "/changeStatus/{id}/{status}")
    public Boolean changeStatus(@LoginUser CurrentUser currentUser, @PathVariable("id") Long id, @PathVariable("status") Integer status) {
        return resourceServiceImpl.changeStatus(id, status, currentUser.getId());
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "资源删除")
    @GetMapping(value = "/del/{id}")
    public Boolean del(@PathVariable("id") Long id) {
        return resourceServiceImpl.del(id);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "根据权限id查询资源id列表")
    @GetMapping(value = "/queryPermissionIdsByResourceId/{resourceId}")
    public List<String> queryPermissionIdsByResourceId(@PathVariable("resourceId") @NotNull(message = "resourceId不能为空") Long resourceId) {
        List<PermissionResourceBO> permissionResourceBos = permissionResourceServiceImpl.queryPermissionResourceByResourceId(resourceId);
        return permissionResourceBos.parallelStream().map(rolePermissionBO -> String.valueOf(rolePermissionBO.getPermissionId())).distinct().collect(Collectors.toList());
    }
}